﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.Xml;
using System.IO;

namespace JKZP
{
    public partial class UserForm : Form
    {
        public UserForm()
        {
            InitializeComponent();
        }

        private void UserForm_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.Clear();
                if ((PP.gUserXml != null) && (PP.gUserXml != ""))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(PP.gUserXml);
                    XmlNodeList nodes = doc.SelectNodes("param/user");
                    if (nodes != null)
                    {
                        foreach (XmlNode node in nodes)
                        {
                            dataGridView1.Rows.Add(new object[] { node.Attributes["username"].Value, node.Attributes["admin"].Value });
                        }
                    }
                }
            }
            catch { }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            AddUserForm form = new AddUserForm();
            form.Owner = this;
            form.ShowDialog();
            form.Dispose();
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要修改的记录");
            }
            else
            {
                EditUserForm form = new EditUserForm();
                form.Owner = this;
                form.rowindex = this.dataGridView1.SelectedRows[0].Index;
                form.username = this.dataGridView1.SelectedRows[0].Cells["username"].Value.ToString();
                form.admin = bool.Parse(this.dataGridView1.SelectedRows[0].Cells["admin"].Value.ToString());
                form.ShowDialog();
                form.Dispose();
            }
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要删除的记录");
            }
            else if (this.dataGridView1.SelectedRows[0].Cells["username"].Value.ToString()=="admin")
            {
                MessageBox.Show("禁止删除超级管理员用户");
            }
            else
            {
                if (PP.kdws.JKZP_DeleteUser(this.dataGridView1.SelectedRows[0].Cells["username"].Value.ToString()) == true)
                {
                    this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);

                    PP.ReLoad();

                    MessageBox.Show("删除成功");

                    
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
